import { encode } from "html-entities";

import iconv from "iconv-lite";

import util from "node:util";

import fs from "node:fs";
import { testFn } from "../utils/common";

async function awaitTest() {
  const data = ["foo", "bar", "!"];
  for await (const val of data) {
    console.log(val);
  }

  const con = encode("< > \"  ™ level ' & © ∆ ™ level", {
    mode: "nonAsciiPrintable",
  });
  console.log("encoded con:", con);

  let text = "时间纷纷";
  text = "LOOK";
  const buf = iconv.encode(text, "utf8");
  const buf2 = iconv.encode(text, "utf16-le");
  console.log("🚀 ~ file: index.ts:31 ~ awaitTest ~ buf:", buf, buf.toString());
  console.log(
    "🚀 ~ file: index.ts:32 ~ awaitTest ~ buf2:",
    buf2,
    buf2.toString("utf-16le")
  );

  // fs.writeFileSync('sample.txt', buf2, { encoding: 'utf-16le'});
  // fs.writeFileSync('./sample.txt', buf2, {encoding: 'utf-8'});
  // fs.writeFileSync('./sample.txt', "\u66f4", { encoding: 'utf-8'});
  // fs.writeFileSync("./sample.txt", "\u66f4", { encoding: "utf16le" });
  fs.writeFileSync("./sample.txt", "\u66f4", { encoding: "base64" });

  // fs.writeFileSync("./sample.txt", "hello", { encoding: "utf16le" });
  // fs.writeFileSync("./sample.txt", "hello", { encoding: "utf8" });

  console.log(iconv.decode(buf, "utf8"));
  console.log(iconv.decode(buf2, "utf16-le"));

  const decoder = new util.TextDecoder("utf-16le");
  const encoder = new util.TextEncoder();
}

export default testFn(awaitTest);
